//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"GameLoadState.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "GameLoadState.h"

#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_DirectInput.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/CSGD_XAudio2.h"
#include "../../SGD Wrappers/CSGD_MessageSystem.h"
#include "../../SGD Wrappers/CSGD_EventSystem.h"
#include "../../SGD Wrappers/SGD_String.h"
#include "../../SGD Wrappers/SGD_Math.h"
#include "../Game.h"
#include <cassert>
#include "../TinyXML/tinyxml.h"
#include "../GameObjects/General.h"
#include "../GameObjects/Berserker.h"
#include "../GameObjects/Knight.h"
#include "../GameObjects/Ranger.h"
#include "../GameObjects/Priest.h"
#include "WorldMapState.h"
#include "../Animation/AnimationManager.h"

GameLoadState* GameLoadState::GetInstance()
{
	static GameLoadState Instance;

	return &Instance;
}
GameLoadState::GameLoadState()
{
	cursor = 0;
}
GameLoadState::~GameLoadState()
{
}
/*virtual*/ void GameLoadState::Enter()
{
	animManager = CAnimationManager::GetInstance();
	titleImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/MapStage3.png"));
	saveImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/map2.png"));
	overwriteBox = false;
}
/*virtual*/ void GameLoadState::Exit()
{

}
/*virtual*/ bool GameLoadState::Input()
{
	CSGD_DirectInput* DI = CSGD_DirectInput::GetInstance();


	if(DI->KeyPressed( DIK_DOWN) == true)
	{
		++cursor;
		if(overwriteBox)
		{
			if(cursor > 1)
				cursor = 1;
		}
		else
		{
			if(cursor > 2)
				cursor = 2;
		}
	}
	else if(DI->KeyPressed(DIK_UP) == true)
	{
		--cursor;

		if(cursor < 0)
			cursor = 0;
	}


	if(DI->KeyPressed(DIK_RETURN) == true && overwriteBox == false)
	{

		saveSlot = cursor;
		overwriteBox = true;
		cursor = 0;

	}
	else if(DI->KeyPressed(DIK_RETURN) == true && overwriteBox == true)
	{
		switch(cursor)
		{
		case 0:
			{
				playerJuan = CreatePlayer();
				LoadGameInfo();
				Game::GetInstance()->PushState(WorldMapState::GetInstance());
				overwriteBox = false;
			}
			break;
		case 1:
			{
				cursor = 0;
				overwriteBox = false;
			}
			break;
		}
	}
	if(DI->KeyPressed(DIK_ESCAPE) == true)
	{
		Game::GetInstance()->PopState();
	}


	return true;

}
/*virtual*/ void GameLoadState::Update( float fElapsedTime )
{

}
/*virtual*/ void GameLoadState::Render()
{
	BitmapFont* Font = Game::GetInstance()->GetFont();
	CSGD_TextureManager::GetInstance()->Draw(titleImgID, 0, 0, 1.0f, 1.0f);

	Font->Draw( _T("CHOOSE A SAVE SLOT"), 
		100, 50, 2.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

	CSGD_TextureManager::GetInstance()->Draw(saveImgID, 300, 150, 1.0f, 1.0f);
	CSGD_TextureManager::GetInstance()->Draw(saveImgID, 300, 300, 1.0f, 1.0f);
	CSGD_TextureManager::GetInstance()->Draw(saveImgID, 300, 450, 1.0f, 1.0f);

	if(overwriteBox == true)
	{
		CSGD_TextureManager::GetInstance()->Draw(titleImgID, 0, 0);
		Font->Draw( _T("OVERWRITE?"), (500 - (4*32)) / 2, 250, 1.0f, D3DCOLOR_ARGB(255,255,255,255));
		Font->Draw( _T("YES"), (500 - (4*32)) / 2, 300, 1.0f, D3DCOLOR_ARGB(255,255,255,255));
		Font->Draw( _T("NO"), (500 - (4*32)) / 2, 350, 1.0f, D3DCOLOR_ARGB(255,255,255,255));

		Font->Draw( _T("(      )"), (500 - (7*32)) / 2, 300 + (cursor * 50), 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	}
	else if(overwriteBox == false)
		Font->Draw( _T("(      )"), (700 - (7*32)) / 2, 200 + (cursor * 150), 2.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

}
void GameLoadState::SaveGameInfo() const
{

}
void GameLoadState::LoadGameInfo()
{
	aniInfo = new AnimInfo;
	const char* szFilename = "CommanderWalking.xml";
	if(animManager->LoadAnimation(szFilename) == false)
		return;

	szFilename = "MeleeStepBack.xml";
	if(animManager->LoadAnimation(szFilename) == false)
		return;

	szFilename = "TankWalk.xml";
	if(animManager->LoadAnimation(szFilename) == false)
		return;


	ostringstream P;
	P << "resource/Save Profiles/Slot" << saveSlot << ".xml";

	TiXmlDocument doc;
	doc.LoadFile(P.str().c_str());
	TiXmlElement* ele = doc.RootElement();
	int j;
	ele->Attribute("Level", &j);
	TiXmlElement* unit = ele->FirstChildElement("Units");
	while(unit)
	{
		int r;
		unit->Attribute("UnitType", &r);
		switch(r)
		{
		case 0:
			{
				General* g = new General;
				g->SetImgID(Game::GetInstance()->GetGeneralImgID());
				g->SetPosX((rand() % 5) * 64 + 32);
				g->SetPosY((rand() % 5) * 64 + 32);


				aniInfo = animManager->GetAnimationInfo(0);
				g->SetAnimInfo(aniInfo);

				g->SetOwner(playerJuan);
				playerJuan->AddUnit(g, playerJuan->Army);
			}
			break;
		case 1:
			{
				Berserker* b = new Berserker;
				b->SetImgID(Game::GetInstance()->GetBerserkerImgID());
				b->SetPosX((rand() % 3) * 512 + 256);
				b->SetPosY((rand() % 5) * 64);
												
				aniInfo = animManager->GetAnimationInfo(1);
				b->SetAnimInfo( aniInfo );

				b->SetOwner(playerJuan);
				playerJuan->AddUnit(b, playerJuan->Army);
			}
			break;
		case 2:
			{
				Knight* k = new Knight;

				aniInfo = animManager->GetAnimationInfo(2);
				k->SetAnimInfo( aniInfo );

				k->SetImgID(Game::GetInstance()->GetKnightImgID());
				k->SetPosX((rand() % 10) * 64);
				k->SetPosY((rand() % 5) * 64);
				k->SetOwner(playerJuan);
				playerJuan->AddUnit(k, playerJuan->Army);
			}
			break;
		case 3:
			{
				Priest* p = new Priest;
				p->SetImgID(Game::GetInstance()->GetPriestImgID());
				p->SetPosX((rand() % 5) * 256);
				p->SetPosY((rand() % 5) * 64);
				p->SetOwner(playerJuan);
				playerJuan->AddUnit(p, playerJuan->Army);
			}
			break;
		case 4:
			{
				Ranger* r = new Ranger;

				r->SetImgID(Game::GetInstance()->GetRangerImgID());
				r->SetPosX((rand() % 5) * 128);
				r->SetPosY((rand() % 5) * 64);
				r->SetOwner(playerJuan);
				playerJuan->AddUnit(r, playerJuan->Army);
			}
			break;
		}
		//unit = 
		unit = unit->NextSiblingElement("Units");
	}

}
Player* GameLoadState::CreatePlayer() const
{
	Player* p = new Player;

	return p;
}